package com.jcgyl.export.controller;

import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcgyl.export.entity.Tbsysmenu;
import com.jcgyl.export.model.sysmenu.QueryModel;
import com.jcgyl.export.service.MenuService;
import com.jcgyl.export.util.entity.JsonResult;
import com.jcgyl.export.util.entity.JsonUtil;
import com.jcgyl.export.util.entity.PageResult;

@Controller
@RequestMapping("/sysmenu/")
public class MenuController {

	@Autowired
	private MenuService menuService;
	/**返回page页面*/
	@RequestMapping("page")
	public String page() {
		return "sysmenu/page";
	}


	/**
	 * 查询全部数据
	 * @return
	 * JL
	 * 实验方法
	 */
	@RequestMapping("doFindObject")
	@ResponseBody
	public PageResult doFindObject(){
		List<Tbsysmenu>list = menuService.findObject();
		int row = menuService.getRowCount();
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(list);
		jsonResult.setTotalRows(row);
		return jsonResult;
	}
	/**
	 * 根据条件查询数据
	 * @param queryModel
	 * @return
	 * JL
	 */
	@RequestMapping("doFindObjects")
	@ResponseBody
	public PageResult doFindObjects(@RequestBody QueryModel queryModel){
		//获取前端传入的数据
		String name = queryModel.getFilter().getPageName();
		String pname = queryModel.getFilter().getPrentName();
		Integer isenable = queryModel.getFilter().getIsEnable();
		Integer pageSize = queryModel.getPageSize();
		Integer pageCurrent = queryModel.getPageIndex();
		List<Tbsysmenu> list = menuService.findMenu(name, pname, isenable,pageSize,pageCurrent);
		int row = menuService.getRow(name, pname, isenable);
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(list);
		jsonResult.setTotalRows(row);
		return jsonResult;
	}
	/**
	 * 查询zTree节点数据
	 * @return
	 * JL
	 */
	@RequestMapping("doFindZtreeObject")
	@ResponseBody
	public JsonResult doFindZtreeObject(){
		List<Map<String,Object>>list = menuService.findZtreeObject();
		return JsonUtil.success(list);
	}
	/**
	 * 修改
	 * @param tbsysmenu
	 * @return
	 * JL
	 */
	@RequestMapping("doUpdateObject")
	@ResponseBody
	public JsonResult doUpdateObject(@RequestBody Tbsysmenu tbsysmenu){
		int row =  menuService.updateObject(tbsysmenu);
		if(row !=1)
			return JsonUtil.error(1, "修改失败！");
		return JsonUtil.success(row);
	}
	/**
	 * 添加
	 * @param tbsysmenu
	 * @return
	 * JL
	 */
	@RequestMapping("doSaveObject")
	@ResponseBody
	public JsonResult doSaveObject(@RequestBody Tbsysmenu tbsysmenu){
		String name =tbsysmenu.getName();
		Integer  row= menuService.saveObject(tbsysmenu);
		if(row!=1)
			return JsonUtil.error(1, "添加失败");
		return JsonUtil.success(row);
	}
	/**
	 * 根据ID删除对象
	 * @param id
	 * @return
	 * JL
	 */
	@RequestMapping("doDeleteObject")
	@ResponseBody
	public JsonResult doDeleteObject(@RequestBody Integer id){
		int row =menuService.deleteObject(id);
		if(row!=1)
			return JsonUtil.error(1, "删除失败！");
		return JsonUtil.success(row);
	}
	/**
	 * 根据id获取单条数据
	 * @param tb
	 * @return
	 * JL
	 */
	@RequestMapping("doFindObjectById")
	@ResponseBody
	public JsonResult doFindObjectById(@RequestBody Tbsysmenu tb){
		Integer id = tb.getId();
		Tbsysmenu tbsysmenu =  menuService.findObjectById(id);
		if(tbsysmenu == null)
			return JsonUtil.error(1, "没有数据信息！");
		return JsonUtil.success(tbsysmenu);

	}
	/**
	 * 修改数据
	 * @param tb
	 * @return
	 * JL
	 */
	@RequestMapping("doUpdataIsenable")
	@ResponseBody
	public JsonResult doUpdataIsenable(@RequestBody Tbsysmenu tb){
		Integer id= tb.getId();
		Integer isenable = tb.getIsenable();
		int row;
		if(isenable == 1){
			isenable = 2;
			 row = menuService.updataIsenable(id, isenable);
		}else{
			isenable =1;
			 row = menuService.updataIsenable(id, isenable);
		}
		return JsonUtil.success(row);
	}
}
